<template>
    <div class="register">
        <div class="left-side">
            <div class="register-content">
                <p class="left-side-tittle">注册</p>
                <ElForm
                    ref="formRef"
                    :model="numberValidateForm"
                    class="register-ruleForm"
                    label-position="top"
                    :rules="rules"
                    :inline="true"
                >
                    <ElFormItem label="账号" prop="account">
                        <ElInput
                            v-model="numberValidateForm.account"
                            class="register-input"
                            type="text"
                            autocomplete="off"
                        ></ElInput>
                    </ElFormItem>

                    <ElFormItem label="密码" prop="password">
                        <ElInput
                            v-model="numberValidateForm.password"
                            class="register-input"
                            type="password"
                            autocomplete="off"
                        ></ElInput>
                    </ElFormItem>
                    <ElFormItem>
                        <ElButton
                            type="primary"
                            class="registerBtn"
                            @click="submitForm"
                        >
                            注册
                        </ElButton>
                        <p class="register-back">
                            已有帐号，
                            <ElButton type="text" class="register-back-btn" @click="backToLogin">马上登录</ElButton>
                        </p>
                    </ElFormItem>
                </ElForm>
            </div>
        </div>
        <div class="right-side fr"></div>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import type { ElForm, FormInstance } from 'element-plus';
import { getRegister } from '@/api/api.query.util';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const formRef = ref<FormInstance>();
const numberValidateForm = reactive({
    account: '',
    password: '',
});

const rules = reactive({
    account: [
        {
            required: true,
            message: '账号不能为空',
            trigger: 'change',
        },
    ],
    password: [
        {
            required: true,
            message: '密码不能为空',
            trigger: 'change',
        },
    ],
});
const router = useRouter();

function backToLogin() {
    router.push({ path: '/login' });
}

function submitForm() {
    getRegister({
        method: 'post',
        data: {
            name: numberValidateForm.account,
            password: numberValidateForm.password
        }
    }).then((res) => {
        console.log(res)
        if(res.code === 401 ) {
            console.log('111')
            ElMessage({
                        message: '用户已存在，请重新注册',
                        type: 'error',
                    });
        }
        if(res.code === 200) {
            ElMessage({
                        message: '注册成功',
                        type: 'success',
                    });
            router.push({ name: '/login' })
        }
    })
}
</script>

<style scoped>
.register {
    display: flex;
    align-items: center;
}
.left-side {
    width: 66vw;
    display: flex;
    justify-content: center;
    align-items: center;
}

.right-side {
    width: 34vw;
    height: 100vh;
    background: url('../../assets/image/register_banner.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

.register-content {
    width: 800px; 
    height: 500px;
}
.left-side-tittle {
    font-size: 26px;
    color: #333;
    font-weight: 600;
    margin-bottom: 35px;
}

.registerBtn {
    width: 592px;
    font-size: 14px;
    background-color: #3ab0a2;
    border-radius: 4px;
    margin-top: 6px;
}

.register-back {
    text-align: center;
    font-family: AdobeHeitiStd-Regular;
    font-size: 14px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 18px;
    letter-spacing: 0px;
    color: #333333;
}

.register-back-btn {
    color: #3ab0a2;
}

.register-input {
    width: 280px;
    height: 39px;
    border-radius: 4px;
}
/deep/.el-form-item__label {
    line-height: 20px;
}
/deep/.el-form--label-top .el-form-item__label {
    padding: 0px;
}

/deep/ .el-form-item__content {
    flex-direction: column;
}
</style>
